Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    question:{

    },
    resultEnd: '',
    options: [],
    next: 0,
    arrayList: '',
    arr: [],
    score: 0,
    timu: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    tt.setNavigationBarColor({
      frontColor: "#000000",
      backgroundColor: "#ffffff",
      success(res) {
        console.log(res);
      },
      fail(res) {
          console.log("setNavigationBarColor调用失败");
        }
    });

    let timuid = options.timuid || "";
    console.log(timuid);
    var that = this;
    that.timu = timuid;
    let tap = tt.request({
      url: 'https://dycps.aikeai.group/testService/findQuestionPage', // 目标服务器url
      data:{
        timuid:  that.timu
      },
      success: (res) => {
        console.log(res,"question");
        // that.timuid = res.data[0].timuid;
        that.arrayList = res.data
        let length1 = res.data[0].options.length;
        console.log(length1,'length1');
      
          that.setData({
            question: res.data[0]
          });  
     
 
      }
    });
  },
  onClickChoice: function (e) {
    var that = this;
    console.log(e,"event")
    
    var index = e.target.id;
    console.log(this.data.question.options[index],"index");
    var nextquestionid = this.data.question.options[index].nextquestionid;
    console.log(nextquestionid,"nextquestionid")
    // console.log(that.arrayList,'arrayList');

    // that.arr.push(this.data.question.options[index].title);
    // console.log(that.arr,"that.arr");
    console.log(this.data.question.options[index].score,'this.data.question.options[index].score')
    var newScore = this.data.question.options[index].score;
    var length = that.arrayList.length;

    if(that.data.arr.length < length){
      that.data.arr.push(newScore)
    }
    console.log( that.data.arr,' that.arr1');

    console.log(length,"length");
    for(let i = 0; i < length; i++){
      if(nextquestionid !== 0) { 
        if(that.arrayList[i].questionid == nextquestionid ){
          var index = i;
          that.setData({
            question: that.arrayList[index]
          });
        }
      }
      
    }
    if(nextquestionid === 0){
        tt.request({
        url: 'https://dycps.aikeai.group/testService/findAnswer', // 目标服务器url
        method: "post",
        data: {
          timuid: that.timu,
          scores:  that.data.arr
        },
        success: (res) => {
          console.log(res,"结果");
          var data = res.data;
          var answer = data.answer;
          var name = data.name;
          
          tt.navigateTo({
            url: `/pages/testResult/testResult?answer=${answer}&name=${name}`,
            success(res) {
              console.log(`${res}`);
            },
            fail(res) {
              console.log(`navigateTo调用失败`);
            }
          });
        }
      });
        

     
    }
   
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})